package cn.kgc.ssm.service.impl;

import cn.kgc.ssm.commons.ResponseResult;
import cn.kgc.ssm.entity.Active;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.mapper.ActiveMapper;
import cn.kgc.ssm.service.ActiveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/5/17
 * @description
 */
@Service
public class ActiveServiceImpl implements ActiveService {

    @Resource
    private ActiveMapper activeMapper;

    @Override
    public ResponseResult activeList() {

        List<Active> actives = activeMapper.selectActiveList();
        return ResponseResult.success().data(actives);
    }

    @Override
    public ResponseResult activePage(Integer page, Integer limit, String publisher) {

        PageHelper.startPage(page, limit);
        List<Active> actives = activeMapper.searchActive(publisher);
        PageInfo<Active> activePageInfo = new PageInfo<>(actives);

        long total = activePageInfo.getTotal();


        return ResponseResult.success().data(actives).count(total);
    }

    @Override
    public ResponseResult addActive(HttpSession session, String content) {

        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        Active active = new Active();
        active.setPublisher(username);
        active.setContent(content);

        activeMapper.insertActive(active);
        return ResponseResult.success().message("活动发布成功");
    }


    @Override
    public ResponseResult getActiveById(Integer id) {
         Active active =   activeMapper.selectActiveById(id);
        return ResponseResult.success().data(active);
    }

    @Override
    public ResponseResult editActive(Active active) {
        activeMapper.editActive(active);
        return ResponseResult.success().message("更新成功");
    }

    @Override
    public ResponseResult deleteActive(Integer[] ids) {

        activeMapper.deleteActives(ids);
        return ResponseResult.success().message("删除成功");
    }
}


